package org.jeecg.modules.bl.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.bl.entity.BlDiscountMeters;
import org.jeecg.modules.bl.service.IBlDiscountMetersService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

 /**
 * @Description: 水费优惠水表
 * @Author: jeecg-boot
 * @Date:   2022-02-13
 * @Version: V1.0
 */
@Api(tags="水费优惠水表")
@RestController
@RequestMapping("/bl/blDiscountMeters")
@Slf4j
public class BlDiscountMetersController extends JeecgController<BlDiscountMeters, IBlDiscountMetersService> {
	@Autowired
	private IBlDiscountMetersService blDiscountMetersService;

	/**
	 * 分页列表查询
	 *
	 * @param blDiscountMeters
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "水费优惠水表-分页列表查询")
	@ApiOperation(value="水费优惠水表-分页列表查询", notes="水费优惠水表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(BlDiscountMeters blDiscountMeters,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<BlDiscountMeters> queryWrapper = QueryGenerator.initQueryWrapper(blDiscountMeters, req.getParameterMap());
		Page<BlDiscountMeters> page = new Page<BlDiscountMeters>(pageNo, pageSize);
		IPage<BlDiscountMeters> pageList = blDiscountMetersService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param blDiscountMeters
	 * @return
	 */
	@AutoLog(value = "水费优惠水表-添加")
	@ApiOperation(value="水费优惠水表-添加", notes="水费优惠水表-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody BlDiscountMeters blDiscountMeters) {

		if(oConvertUtils.isNotEmpty(blDiscountMeters.getCustomerId())){

			int count = blDiscountMetersService.count(new LambdaQueryWrapper<BlDiscountMeters>()
					.in(BlDiscountMeters::getCustomerId, blDiscountMeters.getCustomerId())
					//.eq(BlDiscountMeters::getDiscountId,blDiscountMeters.getDiscountId())
					.eq(BlDiscountMeters::getDelFlag,0));
			if(count>0){
				return Result.error("选择的水表用户已参加水费优惠！");
			}

			String[] cusIds = blDiscountMeters.getCustomerId().split(",");
			String[] cusNames = blDiscountMeters.getCusName().split(",");
			for (int i = 0; i < cusIds.length; i++) {
				BlDiscountMeters meters = new BlDiscountMeters();
				BeanUtils.copyProperties(blDiscountMeters,meters);
				meters.setCustomerId(cusIds[i]);
				meters.setCusName(cusNames[i]);
				blDiscountMetersService.save(meters);
			}
			return Result.OK("添加成功！");
		}
		return Result.error("添加异常！");
	}

	/**
	 *  编辑
	 *
	 * @param blDiscountMeters
	 * @return
	 */
	@AutoLog(value = "水费优惠水表-编辑")
	@ApiOperation(value="水费优惠水表-编辑", notes="水费优惠水表-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody BlDiscountMeters blDiscountMeters) {
		if(oConvertUtils.isNotEmpty(blDiscountMeters.getCustomerId())){
			String[] cusIds = blDiscountMeters.getCustomerId().split(",");
			String[] cusNames = blDiscountMeters.getCusName().split(",");

			boolean remove = blDiscountMetersService.remove(new LambdaQueryWrapper<BlDiscountMeters>()
					.in(BlDiscountMeters::getCustomerId, cusIds)
					//.eq(ArcAdjustedMeters::getAdjustId, arcAdjustedMeters.getAdjustId())
					.eq(BlDiscountMeters::getDelFlag, 0));
			//先删除 再增加

			for (int i = 0; i < cusIds.length; i++) {
				BlDiscountMeters meters = new BlDiscountMeters();
				BeanUtils.copyProperties(blDiscountMeters,meters);
				meters.setCustomerId(cusIds[i]);
				meters.setCusName(cusNames[i]);
				blDiscountMetersService.save(meters);
			}
			return Result.OK("编辑成功！");
		}
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "水费优惠水表-通过id删除")
	@ApiOperation(value="水费优惠水表-通过id删除", notes="水费优惠水表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		blDiscountMetersService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "水费优惠水表-批量删除")
	@ApiOperation(value="水费优惠水表-批量删除", notes="水费优惠水表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.blDiscountMetersService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "水费优惠水表-通过id查询")
	@ApiOperation(value="水费优惠水表-通过id查询", notes="水费优惠水表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		BlDiscountMeters blDiscountMeters = blDiscountMetersService.getById(id);
		if(blDiscountMeters==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(blDiscountMeters);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param blDiscountMeters
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, BlDiscountMeters blDiscountMeters) {
        return super.exportXls(request, blDiscountMeters, BlDiscountMeters.class, "水费优惠水表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, BlDiscountMeters.class);
    }

}
